function random(min,max){
    return Math.floor(min+Math.random()*(max-min));
}
class Plan {
  constructor(hp,x,y,width,height,boomimage,imagesrc) {
    this.x=x;
    this.y=y;
    this.node=null;
    this.hp=hp;
    this.width=width;
    this.height=height;
    this.boomimage=boomimage;
    
    this.node=document.createElement("img");
    this.node.style.left=this.x+"px";
    this.node.style.top=this.y+"px";
    this.node.src=imagesrc;
  }
  moveTo(x,y){
    	this.x = x;
    	this.y = y;
       	this.node.style.left = x+"px";
       	this.node.style.top = y+"px";
    }
}

class Player extends Plan {
	constructor(x, y, color) {
	    super(1,120,495,66,80,"image/bz.gif","image/my.gif");
	    this.color = color;
	}
	shoot(){
		var newbullet = new Bullet(this.x+this.width/2,this.y);
		game.bullets.push(newbullet);
		game.addToGame(newbullet);
	}
}

class Bullet{
	constructor(x,y){
	    this.x=x;
	    this.y=y;
	    this.node = null;
	    this.width=6;
	    this.height=14;
	    this.out = false;
	    this.move=function(){
	        this.node.style.top=this.node.offsetTop-20+"px";
	        if(this.node.offsetTop < 0){
	        	this.out = true;
	        }
	    	this.x = this.node.offsetTop;
	    	this.y = this.node.offsetLeft;
	    }
	    this.node=document.createElement("img");
	    this.node.style.left= this.x+"px";
	    this.node.style.top= this.y+"px";
	    this.node.src="image/bullet1.png";
	}
	move(){
		this.node.style.top=this.node.offsetTop-20+"px";
		if(this.node.offsetTop < 0){
	   		this.out = true;
	    }
	    this.x = this.node.offsetTop;
	    this.y = this.node.offsetLeft;
	}
}


class Enemy extends Plan{
	constructor(width,height,boomimage,imagesrc,speed,hp){
	    super(1,random(0,320-width/2),-100,width,height,boomimage,imagesrc);
	    this.speed = speed;
	    this.hp = hp;
	    this.isdead =false;
	    this.animateTime = 360;
	}
    rush(){
    	this.node.style.top=this.node.offsetTop+this.speed+"px";
        if(this.node.offsetTop > 568){
        	this.out = true;
        }
    	this.x = this.node.offsetTop;
    	this.y = this.node.offsetLeft;

    	if(this.hp <= 0){
			if(this.animateTime <= 0){
				this.isdead = true;
			}else{
				this.animateTime -= 20;
			}
		}
    }
    hert(hert){
    	this.hp -= hert;
    	if(this.hp == 0){
    		this.node.src = this.boomimage;
    	}
    }
}

var game = {
	score:0,
	mainDiv:document.getElementById("maindiv"),
	player:new Player(),
	bullets:[],
	enemys:[],
	frame:0,
	init:function(){
		this.mainDiv.appendChild(this.player.node);
		this.mainDiv.addEventListener("mousemove",function(){
			game.bindMouse();
		});
	},
	addToGame:function(e){
		this.mainDiv.appendChild(e.node)
	},
	bindMouse:function(){
	    var x=event.clientX-this.player.width/2;
	    var y=event.clientY-this.player.height/2;
	    this.player.moveTo(x,y)
	},
	refresh:function(){
		this.frame++;
		//init bullet 
		if(this.frame%5==0){
			this.player.shoot();
		}

		if(this.frame%20==0){
			var newEnemy = new Enemy(34,24,"image/xx.gif","image/enemy1_fly_1.png",random(1,4),1);
			this.enemys.push(newEnemy);
			this.addToGame(newEnemy);
		}

		if(this.frame%50==0){
			var newEnemy = new Enemy(46,60,"image/zz.gif","image/enemy3_fly_1.png",random(1,3),6);
			this.enemys.push(newEnemy);
			this.addToGame(newEnemy);
		}

		if(this.frame%100==0){
			var newEnemy = new Enemy(110,164,"image/dd.gif","image/enemy2_fly_1.png",1,12);
			this.enemys.push(newEnemy);
			this.addToGame(newEnemy);
		}

		for(var b in this.bullets){
			var currentBullet = this.bullets[b];
			currentBullet.move();
			if(currentBullet.out){
            	this.mainDiv.removeChild(currentBullet.node);
				this.bullets.splice(b,1);
			}
			
			for(var e in this.enemys){
				var currentEnemy = this.enemys[e];
				if(currentBullet.node.offsetLeft>currentEnemy.node.offsetLeft&&currentBullet.node.offsetLeft<currentEnemy.node.offsetLeft+currentEnemy.width){
					if(currentBullet.node.offsetTop>currentEnemy.node.offsetTop&&currentBullet.node.offsetTop<currentEnemy.node.offsetTop+currentEnemy.height){
						currentEnemy.hert(1);
					}
				}
			}
		}
		for(var e in this.enemys){
			this.enemys[e].rush();
			
			if(this.enemys[e].out || this.enemys[e].isdead){
            	this.mainDiv.removeChild(this.enemys[e].node);
				this.enemys.splice(e,1);
			}
		}
	}
}
game.init();
setInterval(function(){
	game.refresh();
},20);


